<?php use Model\eventsModel;
    use Controller\Controller;

    class eventsController extends Controller {
        private $EVENT;
        private $ADMIN;

        public function __construct() {
            session_start();
            $this->EVENT = new eventsModel();
        }

        public function getAction($params = null) {
            $response = [];
            if ($this->request()["method"] !== "GET") {
                $this->errorJsonDie(400);
            }
            if ($params === null) {
                $response = $this->EVENT->getAllEvents();
            } else {
                $response = $this->EVENT->getEventById(((int) $params));
            }
            if ($response["status"] == 0){
                $this->errorJsonDie(400, "Not found");
            }
            $this->successJson($response["response"]);
        }
        public function addAction() {
            $this->ADMIN = (isset($_SESSION['admin'])) ? $this->ADMIN = $_SESSION['admin'] : $this->errorJsonDie(403, "unauthorized");
            $request = $this->request();
            $json = $this->request()["json"];
            $response = [];
            if ($this->request()["method"] !== "POST") {
                $this->errorJsonDie(404);
            }
            if (!isset($json["title"])
                || !isset($json["description"])
                || !isset($json["event_data"])
                || !isset($json["place"])
            ){
                $this->errorJsonDie(400, "mismatch params");
            }
            $response = $this->EVENT->addEvent($_SESSION['admin']['id'],
                $json["title"],
                $json["description"],
                $json["event_data"],
                $json["place"]);

            if ($response["status"] == 0) {
                $this->errorJsonDie(400, "Mismatch");
            }
            $this->successJson($response["response"]);
        }
        public function editAction() {
            $this->ADMIN = (isset($_SESSION['admin'])) ? $this->ADMIN = $_SESSION['admin'] : $this->errorJsonDie(403, "unauthorized");
            if ($this->request()["method"] !== "PATCH") {
                $this->errorJsonDie(400);
            }

        }

        public function deleteAction($param = null) {
            $this->ADMIN = (isset($_SESSION['admin'])) ? $this->ADMIN = $_SESSION['admin'] : $this->errorJsonDie(403, "unauthorized");
            $request = $this->request();
            $admin_id = $_SESSION['admin']['id'];
            if ($this->request()["method"] !== "DELETE") {
                $this->errorJsonDie(400, "WTF");
            }
            if ($param === null) {
                $this->errorJsonDie(400, "param is requred");
            }
            $param = (integer) $param;
            $response = $this->EVENT->deleteEvent($admin_id, (integer) $param);
            if ($response["status"] === 0) {
                $this->errorJsonDie(404, "Not found");
            }
            $this->successJson("Delete");
        }
    }